package cn.edu.nju.software.android.model;

import java.io.IOException;
import java.io.Serializable;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import cn.edu.nju.software.android.activity.MainActivity;

import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.RemindAPI;
import com.weibo.sdk.android.net.RequestListener;

public class Message implements Serializable {

	private static final long serialVersionUID = 1L;
	public static RemindAPI api = new RemindAPI(MainActivity.accessToken);
	public static long uid = MainActivity.uid;

	public static void getUnreadMessageNum(final Handler handler) {

		api.unread_count(uid, new RequestListener() {

			@Override
			public void onComplete(String response) {
				// TODO Auto-generated method stub
				JSONObject json = null;
				try {
					json = new JSONObject(response);
					int mention_status = json.getInt("mention_status");
					int mention_cmt = json.getInt("mention_cmt");
					int cmt = json.getInt("cmt");
					android.os.Message msg = new android.os.Message();
					Bundle bundle = new Bundle();
					bundle.putInt("mention_status", mention_status);
					bundle.putInt("mention_cmt", mention_cmt);
					bundle.putInt("cmt", cmt);
					msg.setData(bundle);
					handler.sendMessage(msg);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onIOException(IOException e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onError(WeiboException e) {
				// TODO Auto-generated method stub
			}

		});

	}

	public static void clearUnreadMesssageNum(String type) {
		api.set_count(type, new RequestListener() {

			@Override
			public void onComplete(String response) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onIOException(IOException e) {
				// TODO Auto-generated method stub
				e.printStackTrace();
			}

			@Override
			public void onError(WeiboException e) {
				// TODO Auto-generated method stub
				e.printStackTrace();
			}

		});

	}
}
